using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Zune;

namespace Tetrazune
{
    public class TitleScreen : DrawableGameComponent
    {
        readonly Vector2 ButtonLoc = new Vector2(111, 95);
        readonly Vector2 GlowLoc = new Vector2(105, 89);
        readonly int ButtonSpacing = 47;
        readonly Color SelColor = new Color(255, 250, 187);

        Texture2D background;
        Texture2D[] buttons = new Texture2D[3];
        Texture2D glow;
        Texture2D about;
        Sparkler sparkler;
        bool showingAbout = false;
        SpriteBatch spb;
        int index = 0;
        Zune.Zune zune;

        public event EventHandler GameStart;

        public TitleScreen(Game game, Zune.Zune zune)
            : base(game)
        {
            this.zune = zune;
            this.zune.ButtonUp += new ZuneButtonEventHandler(zune_ButtonUp);
        }

        void zune_ButtonUp(object sender, ZuneButtonEventArgs e)
        {
            if (e.Button == ZuneButtons.Down && !showingAbout)
                index = (index + 1) % 3;
            else if (e.Button == ZuneButtons.Up && !showingAbout)
                index = (index == 0 ? 2 : index - 1);
            if (e.Button == ZuneButtons.Select)
            {
                if (showingAbout)
                {
                    showingAbout = false;
                    return;
                }
                if (index == 0 && this.GameStart != null)
                    this.GameStart(this, new EventArgs());
                else if (index == 1)
                    ; //show high scores
                else if (index == 2)
                    showingAbout = true;
            }
            if (e.Button == ZuneButtons.Back)
            {
                if (!showingAbout)
                    this.Game.Exit();
                else
                    showingAbout = false;
            }
        }

        protected override void LoadContent()
        {
            background = Game.Content.Load<Texture2D>("titleScreen");
            glow = Game.Content.Load<Texture2D>("buttonGlow");
            buttons[0] = Game.Content.Load<Texture2D>("playButton");
            buttons[1] = Game.Content.Load<Texture2D>("highscoresButton");
            buttons[2] = Game.Content.Load<Texture2D>("aboutButton");
            about = Game.Content.Load<Texture2D>("about");
            spb = new SpriteBatch(Game.GraphicsDevice);
            base.LoadContent();
        }
        public override void Draw(GameTime gameTime)
        {
            spb.Begin();
            if (showingAbout)
            {
                spb.Draw(about, Vector2.Zero, Color.White);
            }
            else
            {
                spb.Draw(background, Vector2.Zero, Color.White);
                spb.Draw(glow, new Vector2(GlowLoc.X, GlowLoc.Y + ButtonSpacing * index), Color.White);
                for (int i = 0; i < 3; i++)
                {
                    spb.Draw(
                        buttons[i],
                        new Vector2(ButtonLoc.X, ButtonLoc.Y + ButtonSpacing * i),
                        (i == index ? SelColor : Color.White));
                }
            }
            spb.End();
            base.Draw(gameTime);
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

    }
}